package ro.ubbcluj.cs.damate.managers.administration;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import ro.ubbcluj.cs.damate.managers.BaseView;
import ro.ubbcluj.cs.damate.persistence.entities.User;
import ro.ubbcluj.cs.damate.services.UserService;
import ro.ubbcluj.cs.damate.services.mail.MailService;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Lóránd Fazakas (lfazakas)
 *         Date: 2013.03.25.
 *         Time: 22:39
 */
@Component
@Scope("view")
public class InactiveUserManager extends BaseView implements Serializable {

    @Autowired
    private UserService userService;

    @Autowired
    private MailService mailService;

    private List<UserMapper> inactiveUsers;
    private List<UserMapper> filteredInactiveUsers;

    private boolean selectAllBox;
    private boolean statusSwitch;

    private void clean() {
        this.inactiveUsers = null;
        this.filteredInactiveUsers = null;
        this.statusSwitch = false;
        this.selectAllBox = false;
    }

    public void activate() {
        changeState(true);
        clean();
    }

    public void deactivate() {
        changeState(false);
        clean();
        this.statusSwitch = true;
    }

    private void changeState(boolean state) {
        for(UserMapper user : inactiveUsers) {
            if (user.isChecked()) {
                user.getUser().setState(state);
                userService.update(user.getUser());
                if (state) {
                    mailService.sendAccountActivation(user.getUser());
                }  else {
                    mailService.sendAccountDeactivation(user.getUser());
                }
            }
        }
    }

    public void selectAll(boolean state){
        this.selectAllBox = state;
        for(UserMapper user : inactiveUsers) {
            user.setChecked(selectAllBox);
        }
    }

    public void switchState(boolean state) {
        clean();
        this.statusSwitch = state;
    }

    public List<UserMapper> getInactiveUsers() {
        if (inactiveUsers == null) {
            this.inactiveUsers = new ArrayList<UserMapper>();
            for(User user : userService.findAllByState(statusSwitch)) {
                inactiveUsers.add(new UserMapper(user));
            }
        }
        return inactiveUsers;
    }

    public void setInactiveUsers(List<UserMapper> inactiveUsers) {
        this.inactiveUsers = inactiveUsers;
    }

    public List<UserMapper> getFilteredInactiveUsers() {
        return filteredInactiveUsers;
    }

    public void setFilteredInactiveUsers(List<UserMapper> filteredInactiveUsers) {
        this.filteredInactiveUsers = filteredInactiveUsers;
    }

    public boolean isStatusSwitch() {
        return statusSwitch;
    }

    public void setStatusSwitch(boolean statusSwitch) {
        this.statusSwitch = statusSwitch;
    }

    public boolean isSelectAllBox() {
        return selectAllBox;
    }

    public void setSelectAllBox(boolean selectAllBox) {
        this.selectAllBox = selectAllBox;
    }
}
